﻿using System;
using System.Collections.Generic;
using System.Text;
//new
using System.Xml;
using JRCore.tools;
using System.Xml.Serialization;
using System.IO;

namespace JRCore.protocol
{
    /// <summary>
    /// 设备同步
    /// </summary>
    [Serializable]
    public class DeviceSyn
    {
        /// <summary>
        /// 数据终端IP
        /// </summary>
        [XmlElement("DataRecvIP")]
        public string DataRecvIP;

        /// <summary>
        /// 地点信息条数
        /// </summary>
        [XmlElement("Count")]
        public int Count;

        /// <summary>
        /// 路口列表
        /// </summary>
        [XmlArray("DeviceNodeList"), XmlArrayItem("DeviceNode")]
        public List<DeviceNode> DeviceNodeList;

        /// <summary>
        /// DeviceSyn
        /// </summary>
        public DeviceSyn()
        {
            DataRecvIP = "";
            Count = 0;
            DeviceNodeList = new List<DeviceNode>();
        }

        /// <summary>
        /// 转化为XML
        /// </summary>
        /// <returns></returns>
        public string ToXml()
        {
            string ans = "";
            MemoryStream ms = null;
            try
            {
                XmlSerializerNamespaces nss = new XmlSerializerNamespaces();
                nss.Add("", "");//去除根节点的namespace内容
                XmlWriterSettings set = new XmlWriterSettings();
                set.Encoding = new UTF8Encoding(false);
                set.Indent = true;

                ms = new MemoryStream();
                using (XmlWriter xmlWriter = XmlWriter.Create(ms, set))
                {
                    XmlSerializer xz = new XmlSerializer(typeof(DeviceSyn));
                    xz.Serialize(xmlWriter, this, nss);
                }
                ans = Encoding.UTF8.GetString(ms.ToArray());
                ms.Close();
                ms.Dispose();
            }
            catch (Exception er)
            {
                if (ms != null)
                {
                    ms.Close();
                    ms.Dispose();
                }
                ans = "";
                Console.WriteLine("DeviceSyn.ToXML()" + er.Message);
            }
            return ans;
        }

        /// <summary>
        /// 对象转换为bytes用于TCP发送
        /// </summary>
        /// <returns>内存数组</returns>
        public byte[] ToBytes()
        {
            byte[] ans = null;
            try
            {
                ans = Encoding.UTF8.GetBytes(ToXml());
            }
            catch (Exception er)
            {
                ans = null;
                Console.WriteLine("DeviceSyn.ToBytes()" + er.Message);
            }
            return ans;
        }

        /// <summary>
        /// 由内存构造对象
        /// </summary>
        /// <param name="data">数据</param>
        /// <returns>对象</returns>
        public static DeviceSyn Parse(byte[] data)
        {
            DeviceSyn ans = null;
            MemoryStream ms = null;
            try
            {
                ms = new MemoryStream(data);
                XmlSerializer z = new XmlSerializer(typeof(DeviceSyn));
                ans = (DeviceSyn)z.Deserialize(ms);
            }
            catch (Exception er)
            {
                ans = null;
                Console.WriteLine("DeviceSyn.Parse()" + er.Message);
            }
            finally
            {
                if (ms != null)
                {
                    ms.Close();
                    ms.Dispose();
                }
            }
            return ans;
        }
    }
}
